package com.changcheng.project.android.controller;

import com.changcheng.framework.aspectj.lang.annotation.Log;
import com.changcheng.framework.aspectj.lang.enums.BusinessType;
import com.changcheng.framework.web.domain.AjaxResult;

import com.changcheng.project.android.domain.AndroidBean;
import com.changcheng.project.android.domain.OBU;
import com.changcheng.project.android.domain.Obu_Updata;

import com.changcheng.project.obupublish.domain.AObuPublish;
import com.changcheng.project.obupublish.service.IAObuPublishService;
import com.changcheng.project.obuupgradelogs.domain.AObuUpgradelogs;
import com.changcheng.project.obuupgradelogs.service.IAObuUpgradelogsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Api(description = "Obu升级")
@RestController
@RequestMapping("/etcAfterSale")
public class AndroidOBU {
    @Autowired
    private IAObuPublishService aObuPublishService;
    @Autowired
    private IAObuUpgradelogsService aObuUpgradelogsService;

    /**
     * 查询OBU是否需要升级 0是失败  1是成功
     */
    @ApiOperation(value = "obu升级查询",notes = "obu升级查询")
    @PostMapping("/obuUpdateQuery")
    public AndroidBean obuUpdateQuery(@RequestBody Obu_Updata obu_updata) {
        AndroidBean androidBean = new AndroidBean();

        //当前OBU的版本
        String obuversion = obu_updata.getCurrentVersion();
        //当前OBU的厂家
        String obuVendor = obu_updata.getObuVendor();
        AObuPublish aObuPublish1 = new AObuPublish();
        if (obuVendor.equals("0")) {
            aObuPublish1.setObuPublishFactory("金溢");
        } else if (obuVendor.equals("1")){
            aObuPublish1.setObuPublishFactory("聚利");
        }

        List<AObuPublish> list = aObuPublishService.selectAObuPublishList(aObuPublish1);
        if (list != null && list.size() > 0) {
            //有厂家的数据
            AObuPublish aObuPublish = list.get(0);
            String pingtaiversion = aObuPublish.getObuPublishVersion();
            if (!pingtaiversion.equals(obuversion)) {

                AObuUpgradelogs aObuUpgradelogs = new AObuUpgradelogs();
                aObuUpgradelogs.setOperName(obu_updata.getOperName());
                aObuUpgradelogs.setOperTime(obu_updata.getOperTime());
                aObuUpgradelogs.setStationCode(obu_updata.getStationCode());
                aObuUpgradelogs.setStationName(obu_updata.getStationName());
                aObuUpgradelogs.setDeviceId(obu_updata.getDeviceId());
                aObuUpgradelogs.setObuVendor(aObuPublish1.getObuPublishFactory());
                aObuUpgradelogs.setObuMac(obu_updata.getObuMac());
                aObuUpgradelogs.setIcNumber(obu_updata.getIcNumber());
                aObuUpgradelogs.setObuSn(obu_updata.getObuSn());
                aObuUpgradelogs.setCurrentVersion(obu_updata.getCurrentVersion());
                aObuUpgradelogs.setAppVersion(obu_updata.getAppVersion());
                aObuUpgradelogs.setPreUpgradeVersion(pingtaiversion);
                //记录id
                String recordId = UUID.randomUUID().toString().replace("-", "").substring(0, 6);
                aObuUpgradelogs.setRecordId(recordId);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy‐MM‐dd a hh:mm:ss");
                aObuUpgradelogs.setUpgradeTime(sdf.format(new Date()));
                aObuUpgradelogs.setUpgradeStatus(null);
                aObuUpgradelogsService.insertAObuUpgradelogs(aObuUpgradelogs);

                //有升级数据
                androidBean.setCode(0);
                androidBean.setMessage("查询成功，有最新程序，请下载！");
                OBU obu = new OBU();
                obu.setIsNeedUpdate("0");
                obu.setNewVersion(aObuPublish.getObuPublishVersion());
                String url = aObuPublish.getObuPublishUrl();
                url = url.substring(url.indexOf("/pro"), url.length());
                obu.setUpdateUrl(url);
                obu.setRecoudId(recordId);
                androidBean.setObject(obu);
            } else {
                //无升级数据
                androidBean.setCode(0);
                androidBean.setMessage("查询成功，OBU程序已是最新，暂无升级！");
                OBU obu = new OBU();
                obu.setIsNeedUpdate("1");
                androidBean.setObject(obu);
            }

        } else {
            androidBean.setCode(0);
            androidBean.setMessage("查询成功，暂无发布数据！");
            OBU obu = new OBU();
            obu.setIsNeedUpdate("1");
            androidBean.setObject(obu);
        }
        return androidBean;
    }

    /**
     * 新增保存OBU升级记录
     */
    @ApiOperation(value = "obu升级结果", notes = "obu升级结果")
    @Log(title = "OBU升级记录", businessType = BusinessType.INSERT)
    @PostMapping("/obuUpgradeResult")
    public AndroidBean addSave(@RequestBody AObuUpgradelogs aObuUpgradelogs) {
        AndroidBean androidBean = new AndroidBean();
        try {
            AObuUpgradelogs aObuUpgradelogs1 = new AObuUpgradelogs();
            //升级结果
            aObuUpgradelogs1.setUpgradeStatus(aObuUpgradelogs.getUpgradeStatus());
            //结束升级时间
            aObuUpgradelogs1.setUpgradeTime(aObuUpgradelogs.getUpgradeTime());
            //记录id
            aObuUpgradelogs1.setRecordId(aObuUpgradelogs.getRecordId());
            aObuUpgradelogsService.updateAObuUpgradelogs(aObuUpgradelogs1);
            androidBean.setCode(0);
            androidBean.setMessage("上传成功！");
        } catch (Exception e) {
            androidBean.setCode(1);
            androidBean.setMessage("上传失败！"+e);
        }
        return androidBean;
    }
}
